package com.lhj.springboot.rpc.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import javax.swing.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author lhj
 * @Date 2024/7/21 17:06
 */
@Slf4j
@Service
public class WeatherService {


    public ResponseEntity<?> getWeather(String cityName) {
        log.info("请求参数: {}", cityName);
        //1. 创建webClient对象
        WebClient webClient = WebClient.create();

        Map<String, String> params = new HashMap<>();
        params.put("cityName", cityName);
        
        //2. 发送请求
        Mono<String> mono = webClient.get()
                .uri("https://weather110.market.alicloudapi.com/getWeather_40day?cityName=｛cityName｝", params)
                .accept(MediaType.APPLICATION_JSON)
                .header("Authorization", "APPCODE 72ecffb9a42d41f7941cacd582ae556e")
                .retrieve()
                .bodyToMono(String.class);
        
        //3. 返回结果
        return ResponseEntity.ok(mono);
    }

    public ResponseEntity<?> noticePageList() {
        //1. 创建WebClient对象
        WebClient client = WebClient.create();
        
        Map<String, String> params = new HashMap<>();
        params.put("readStatus", "0");

        Mono<String> mono = client.post().uri("http://1.14.94.158:9999/smartschool/home/app/family/notice/pageList")
                .accept(MediaType.APPLICATION_JSON)
                .bodyValue(params)
                .header("Authorization", "Bearer f75dd1c0-04a2-49ee-908b-97aa9a571745")
                .retrieve()
                .bodyToMono(String.class);

        return ResponseEntity.ok(mono);
    }
}
